import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useTeachersStore = defineStore('teachers',() => {
    // 模拟老师数据
    const teachers = ref([
            {
              "id": 1,
              "teacherName": "张老师",
              "phoneNum": "15098765432",
              "gender": "女",
              "birthDate": "2020-09-07",
              "intro": "骨干教师",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "教spring框架",
              "email": "3496351038@qq.com",
            },
            {
              "id": 2,
              "teacherName": "李老",
              "phoneNum": "18612345678",
              "gender": "男",
              "birthDate": "2020-09-09",
              "intro": "骨干教师",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "教前端三剑客",
              "email": "3496351038@qq.com",
            },
            {
              "id": 3,
              "teacherName": "黑衣人",
              "phoneNum": "15912345678",
              "gender": "男",
              "birthDate": "2020-09-25",
              "intro": "骨干教师",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "全能",
              "email": "3496351038@qq.com",
            },
            {
              "id": 4,
              "teacherName": "魏老",
              "phoneNum": "13912345678",
              "gender": "男",
              "birthDate": "2020-09-26",
              "intro": "骨干教师",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "微服务开发",
              "email": "3496351038@qq.com",
            },
            {
              "id": 8,
              "teacherName": "周老",
              "phoneNum": "13712345678",
              "gender": "男",
              "birthDate": "2021-12-13",
              "intro": "骨干教师",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "教linux",
              "email": "3496351038@qq.com",
            },
            {
              "id": 9,
              "teacherName": "吴老",
              "phoneNum": "15812345678",
              "gender": "男",
              "birthDate": "1995-02-02",
              "intro": "骨干教师",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "教数据库",
              "email": "3496351038@qq.com",
            },
            {
              "id": 15,
              "teacherName": "admin",
              "phoneNum": "15212345678",
              "gender": "女",
              "birthDate": "2000-02-02",
              "intro": "测试老师介绍",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "测试老师备注",
              "email": "test@126.com",
            },
            {
              "id": 17,
              "teacherName": "jack",
              "phoneNum": "18912345678",
              "gender": "男",
              "birthDate": "",
              "intro": "",
              "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
              "remark": "",
              "email": "test@126.com",
            },
            {
              "id": 19,
              "teacherName": "测试删除老师",
              "phoneNum": "15588884444",
              "gender": "女",
              "birthDate": "1998-12-28",
              "intro": "测试删除老师介绍",
              "url": "",
              "remark": "测试删除老师备注",
              "email": "test@126.com",
            }])

    const addTeacher = (teacher) => {
        teachers.value.push({
            "id": teachers.value.length + 1,
            "teacherName": teacher.teacherName,
            "phoneNum": teacher.phoneNum,
            "email": teacher.email,
            "gender": teacher.gender,
            "birthDate": teacher.birthDate,
            "date": getFormattedDate(),
            "intro": teacher.intro,
            "remark": teacher.remark
        });
    }

    const editTeacher = (teacher) => {
        const temp = teachers.value.find(item => item.id === teacher.id);
        temp.teacherName = teacher.teacherName;
        temp.phoneNum = teacher.phoneNum;
        temp.email = teacher.email;
        temp.gender = teacher.gender;
        temp.birthDate = teacher.birthDate;
        temp.intro = teacher.intro;
        temp.remark = teacher.remark;
    }

    const deleteTeacher = (id) => {
        const index = teachers.value.findIndex(item => item.id === id);
        teachers.value.splice(index, 1);
    }

    const getFormattedDate = () => {
        const date = new Date(Date.now());
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
    }

    return {teachers, 
        addTeacher,
        editTeacher,
        deleteTeacher,
    }
})

// import { defineStore } from 'pinia';
// import { ref } from 'vue'
// import axios from 'axios';

// export const useTeachersStore = defineStore({
//   id: 'teachers',
//   state: () => ({
//     teachers: [],
//   }),
//   actions: {
//     async fetchTeachers() {
//       try {
//         const response = await axios.get('https://api.example.com/teachers');
//         this.teachers = response.data;
//       } catch (error) {
//         console.error('Error fetching teachers:', error);
//       }
//     },
//     async addTeacher(teacher) {
//       try {
//         const response = await axios.post('https://api.example.com/teachers', teacher);
//         this.teachers.push(response.data);
//       } catch (error) {
//         console.error('Error adding teacher:', error);
//       }
//     },
//     async editTeacher(updatedTeacher) {
//       try {
//         const response = await axios.put(`https://api.example.com/teachers/${updatedTeacher.id}`, updatedTeacher);
//         // 更新本地状态
//         const index = this.teachers.findIndex(t => t.id === updatedTeacher.id);
//         if (index !== -1) {
//           this.teachers[index] = response.data;
//         }
//       } catch (error) {
//         console.error('Error editing teacher:', error);
//       }
//     },
//     async deleteTeacher(id) {
//       try {
//         await axios.delete(`https://api.example.com/teachers/${id}`);
//         // 移除本地状态
//         this.teachers = this.teachers.filter(t => t.id !== id);
//       } catch (error) {
//         console.error('Error deleting teacher:', error);
//       }
//     },
//   },
// });